package adapter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

import app.Application;
import bean.MainFunBean;
import shanshuitiancheng.news.R;

/**
 * Created by cch on 2016/6/20.
 */
public class MainAdapter extends BaseAdapter {
    private Context ct;
    private List<MainFunBean.NewsBean> mlist;

    public MainAdapter(List<MainFunBean.NewsBean> mlist, Context ct) {
        this.mlist = mlist;
        this.ct = ct;
    }

//    @Override
//    public int getViewTypeCount() {
//        return 3;
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        return position;
//    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder = null;
//        if(convertView == null){
//            convertView = LayoutInflater.from(ct).inflate(R.layout.functionview,null);
//            holder = new ViewHolder();
//            holder.image = (ImageView) convertView.findViewById(R.id.image);
//            holder.title = (TextView) convertView.findViewById(R.id.tv1);
//            holder.description = (TextView) convertView.findViewById(R.id.tv2);
//            convertView.setTag(holder);
//        }else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//        MainFunBean.NewsBean newsBean = mlist.get(position);
//        String title = newsBean.getTitle();
//        String des = newsBean.getWriter();
//        List<String> imgs = newsBean.getImgs();
//        if(imgs.size()>0){
//            String s = imgs.get(0);
//            Application.universalImageLoader.displayImage(s,holder.image,Application.options);
//        }
//
//        holder.title.setText(title);
//        holder.description.setText(des);
//        return convertView;

            ViewHolder holder = null;
        if (convertView == null){
           convertView = LayoutInflater.from(ct).inflate(R.layout.functionview,null);
            holder = new ViewHolder();
            holder.imag = (ImageView) convertView.findViewById(R.id.image);
            holder.title = (TextView) convertView.findViewById(R.id.tv1);
            holder.description = (TextView) convertView.findViewById(R.id.tv2);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//        MainFunBean.NewsBean ness = mlist.get(position);
        String title = mlist.get(position).getTitle();
        String des = mlist.get(position).getWriter();
        List<String> ima = mlist.get(position).getImgs();
        String litPic = mlist.get(position).getLitpic();
        if (ima!=null && ima.size() > 0) {
            String s = ima.get(0);
            Application.universalImageLoader.displayImage(s,holder.imag,Application.options);
        }else if(!TextUtils.isEmpty(litPic)){
            Application.universalImageLoader.displayImage(litPic,holder.imag,Application.options);
        }
//
//
// else if(!TextUtils.isEmpty(litPic)){
//            Application.universalImageLoader.displayImage(litPic,holder.imag,Application.options);
//        }
        holder.title.setText(title);
        holder.description.setText(des);
        return convertView;
    }

    class ViewHolder{
        private ImageView imag;
        private TextView title,description;
    }


//    class ViewHolder{
//        private ImageView image;
//        private TextView title,description;
//    }
}
